package com.pac.role.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.common.page.Page;
import com.common.page.PageContext;
import com.common.util.LogicException;
import com.common.util.SessionKeyContent;
import com.pac.role.bean.RoleBean;
import com.pac.role.service.RoleService;
import com.pac.user.UserKeyContent;
import com.pac.user.bean.UserBean;

/**
 * 角色Controller
 * 
 * @author lujun.chen
 * @version [版本号, 2015年2月16日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Controller
@Scope("prototype")
@RequestMapping("/role")
public class RoleController {

	private static Logger logger = Logger.getLogger(RoleController.class);

	@Autowired
	private RoleService roleService;

	/**
	 * 列表查询角色
	 * 
	 * @param roleBean
	 *            查询条件
	 * @param page
	 *            下一页页码
	 * @param rows
	 *            每页记录条数
	 * @return roleList 角色列表
	 * @see [类、类#方法、类#成员]
	 */
	@RequestMapping("/listData")
	@ResponseBody
	public Page queryRoleList(@ModelAttribute RoleBean roleBean, int page,
			int rows) {
		// 设置每页记录数
		PageContext.getContext().setPageSize(rows);
		// 跳转到指定页
		PageContext.getContext().gotoPage(page);

		List<RoleBean> roleBeanList = roleService.queryRole(roleBean);

		Page currentPage = new Page();
		// 总行数
		int totlaRols = PageContext.getContext().getTotal();
		currentPage.setTotal(totlaRols);
		currentPage.setRows(roleBeanList);

		return currentPage;
	}

	/**
	 * 页面跳转
	 * 
	 * @param pageName
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	@RequestMapping("/gotoPage")
	public ModelAndView gotoPage(String pageName, String userId) {
		ModelAndView mov = new ModelAndView();
		mov.addObject("userId", userId);
		mov.setViewName("role/" + pageName);
		return mov;
	}

	/**
	 * 添加角色
	 * 
	 * @param roleBean
	 *            参数
	 * @return result
	 * 
	 *         true：添加成功
	 * 
	 *         false：添加失败
	 * 
	 *         msg：提示信息
	 * 
	 * @see [类、类#方法、类#成员]
	 */
	@RequestMapping("/add")
	@ResponseBody
	public Map<String, Object> addRole(@ModelAttribute RoleBean roleBean) {
		boolean result = true;
		String msg = "";
		try {
			roleService.addRole(roleBean);
			msg = "新增角色【" + roleBean.getRoleName() + "】成功";
		} catch (LogicException e) {
			result = false;
			msg = e.getMessage();
			logger.error("新增角色出错：" + e.getMessage());
		} catch (Exception e) {
			result = false;
			msg = "新增角色出错，系统内部错误";
			logger.error("新增角色出错：" + e);
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", result);
		map.put("msg", msg);

		return map;
	}

	/**
	 * 删除角色信息
	 * 
	 * @param roleId
	 *            ：角色ID
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Map<String, Object> delRole(String roleId) {
		boolean result = true;
		String msg = "";

		try {
			roleService.delRoleById(roleId);
			msg = "删除角色成功";
		} catch (LogicException e) {
			result = false;
			msg = e.getMessage();
			logger.error("删除角色出错：" + e.getMessage());
		} catch (Exception e) {
			result = false;
			msg = "删除角色出错，系统内部错误";
			logger.error("删除角色出错：" + e);
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", result);
		map.put("msg", msg);

		return map;
	}

	/**
	 * 修改角色
	 * 
	 * @param roleBean
	 *            角色信息
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	@RequestMapping("/edit")
	@ResponseBody
	public Map<String, Object> editRole(@ModelAttribute RoleBean roleBean) {
		boolean result = true;
		String msg = "";

		try {
			roleService.updateRole(roleBean);
			msg = "修改角色成功";
		} catch (LogicException e) {
			result = false;
			msg = e.getMessage();
			logger.error("修改角色出错：" + e.getMessage());
		} catch (Exception e) {
			result = false;
			msg = "修改角色出错，系统内部错误";
			logger.error("修改角色出错：" + e);
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", result);
		map.put("msg", msg);

		return map;
	}
	
	/**
	 * 查询所有角色
	 * 
	 * @param userId
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	@RequestMapping("/all")
	@ResponseBody
	public List<RoleBean> queryRole(HttpServletRequest request) {
		// system用户查询全部角色
		// 非system用户只能查询我拥有的角色
		HttpSession session = request.getSession();
		UserBean userBeans = (UserBean) session
				.getAttribute(SessionKeyContent.SESSION_KEY_OBJ_USER_BEAN);
		RoleBean roleBean = new RoleBean();
		if (!UserKeyContent.USER_ID_SYSTEM.equals(userBeans.getId())) {
			roleBean.setUserId(userBeans.getId());
		}

		List<RoleBean> roleBeanList = roleService.queryRoleAll(roleBean);

		return roleBeanList;
	}

}
